package com.zlx.rulemk.datagen;

import com.typesafe.config.ConfigFactory;
import com.zlx.rulemk.marketing.constant.ConfigNames;
import com.zlx.rulemk.marketing.utils.ConnectionUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author: zhanglingxing
 * @CreateTime: 2022-07-06
 * @Description: 模拟生成Hbase的画像数据
 * @Version: 1.0
 *
 * create 'zlx_profile','f'
 *
 * hbase shell
 * scan 'zlx_profile',{LIMIT=>10}
 */
public class _02_UserProfileDataGen {

    public static void main(String[] args) throws IOException {

        Connection conn = ConnectionUtils.getHbaseConnection();
        TableName htable = TableName.valueOf(ConfigFactory.load().getString(ConfigNames.HBASE_PROFILE_TABLE));

        Table table = conn.getTable(htable);

        ArrayList<Put> puts = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {

            // 生成一个用户的画像标签数据
            String deviceId = StringUtils.leftPad(i + "", 6, "0");
            Put put = new Put(Bytes.toBytes(deviceId));
            for (int k = 1; k <= 100; k++) {
                String key = "tag" + k;
                String value = "v" + RandomUtils.nextInt(1, 3);
                put.addColumn(Bytes.toBytes("f"), Bytes.toBytes(key), Bytes.toBytes(value));
            }

            // 将这一条画像数据，添加到list中
            puts.add(put);

            // 攒满100条一批
            if(puts.size()==100) {
                table.put(puts);
                puts.clear();
            }

        }

        // 提交最后一批
        if(puts.size()>0) table.put(puts);

        conn.close();
    }
}
